<?PHP
global $relPath;
include_once($relPath.'dpinit.php');

function abort( $error_message ) {
    echo "<p>";
    echo _("We are unable to complete your request.");
    echo "</p>\n";
    echo "<p>";
    echo _("If you want to resolve, report, or get help with this incident, it may be useful to save the information appended below.");
    echo "</p>\n";

    echo "\n";
    provide_escape_links();
    echo "\n";

    echo "<hr>\n";

    echo "<pre>\n";
    echo _("error message"), ": $error_message\n";
    echo "\n";
    dump_request_info();

	global $pguser;
	if($pguser == 'dkretz') {
		StackDump();
		die();
	}
    echo "</pre>\n";
    exit;
}

function provide_escape_links() {
    global $code_url;

    echo "<p>\n";
    echo _('Go to...'), "\n";
    echo "<ul>\n";

    // link to project home
    $projectid = get_parameter_value( array('projectid', 'projectname', 'project') );
    if ( !is_null($projectid) ) {
        // This link won't work if the error is that the project doesn't exist.
        $link_url = "$code_url/project.php?id=$projectid";
        $link_text = _('Project Home');
        echo "<li><a href='$link_url' target='_top'>$link_text</a></li>\n";
    }

    // link to round
    $round = get_Round_for_project_state(
        get_parameter_value( array('proj_state', 'proofstate') ) );
    if ( is_null($round) ) {
        $round = get_Round_for_page_state(
            get_parameter_value( array('pagestate', 'page_state') ) );
    }
    if ( !is_null($round) ) {
        $link_url = "$code_url/tools/proofers/round.php?round_id={$round->id}";
        $link_text = $round->name;
        echo "<li><a href='$link_url' target='_top'>$link_text</a></li>\n";
    }

    // link to activity hub
    $link_url = "$code_url/activity_hub.php";
    $link_text = _('Activity Hub');
    echo "<li><a href='$link_url' target='_top'>$link_text</a></li>\n";
    echo "</ul>\n";
    echo "</p>\n";
}

// Look through request parameters for a parameter with one of the given names
// and a non-empty value. Return the value, or NULL if none found.
function get_parameter_value( $possible_names ) {
    foreach ( $possible_names as $possible_name ) {
        $v = @$_GET[$possible_name];
        if ( $v != '' )
            return $v;

        $v = @$_POST[$possible_name];
        if ( $v != '' )
            return $v;
    }
    return NULL;
}

function dump_request_info() {
    $now = time(); $date = date('Y-m-d H:i:s',$now);
    echo "time: $now ($date)\n";
    echo "username: {$GLOBALS['pguser']}\n";
    echo "requested path: {$_SERVER['PHP_SELF']}\n";

    // If this error occurred when they hit one of the submit buttons
    // in the proofing interface, echoing the request parameters will
    // give them a last chance to save the page text.

    foreach ( array('_GET', '_POST') as $array_name ) {
        echo "$array_name:\n";
        switch($array_name) {
            case '_GET':
                $ary = $_GET;
                break;
            case '_POST':
                $ary = $_POST;
                break;
            default:
                $ary = array();
                assert(FALSE);
        }

        // var_dump($array);

        if (count($ary) == 0) {
            echo "    (empty)\n";
            continue;
        }
        foreach( $ary as $param_name => $param_value ) {
            echo "    $param_name = ";
            if ( is_string($param_value) ) {
                $unslashed_param_value =
                    get_magic_quotes_gpc()
                    ? stripslashes($param_value)
                    : $param_value;

                $htmlescaped_param_value =
                    htmlspecialchars($unslashed_param_value,ENT_NOQUOTES);

                if ( mb_strpos($param_value, "\n") ) {
                    echo "'''\n";
                    echo $htmlescaped_param_value, "\n";
                    echo "'''\n";
                }
                else {
                    echo "'$htmlescaped_param_value'\n";
                }
            }
            else {
                // might be an array.
                // This will be ugly, but I don't care.
                var_dump($param_value);
            }
        }
    }
}

// vim: sw=4 ts=4 expandtab
